package com.example.excelimport.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ParsXml
 * @DESCRIPT: TODO
 * @AUTHOR: cuizhichao
 * @DATA: 2020/1/17 4:44 PM
 **/
public class ParsXml {

    public static List<Map<String,String>> getConfig(String path){

        SAXReader saxReader = new SAXReader();
        InputStream inputStream = ParsXml.class.getResourceAsStream(path);
        Document document = null;
        try {
            document = saxReader.read(inputStream);
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        }
        String xpath = "//Column";
        List<Element> list = document.selectNodes(xpath);
        List<Map<String,String>> parasList = new ArrayList<>();
        if(list != null){
            for (Element col:list) {
                Map<String,String> map = new HashMap<>();
                String group = col.getParent().attributeValue("group");
                String value = col.attributeValue("value");
                String name = col.element("name").getData().toString();
                String regContent = col.element("regContent").getData().toString();
                String errorMsg = col.element("errorMsg").getData().toString();
                map.put("name",name);
                map.put("regContent",regContent);
                map.put("errorMsg",errorMsg);
                map.put("group",group);
                map.put("value",value);
                parasList.add(map);
            }
        }
        return parasList;
    }
}
